package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"medi-reserve/cecha"
	"medi-reserve/medirpc/internal/svc"
	"medi-reserve/medirpc/medirpc"
	"medi-reserve/model"
	"time"
)

type ReserveListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReserveListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReserveListLogic {
	return &ReserveListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReserveListLogic) ReserveList(in *medirpc.ReserveListRequest) (*medirpc.ReserveListResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("List_%s", in)
	var list []*medirpc.ReserveList
	result, _ := cecha.Rdb.Get(cecha.Ctx, key).Result()
	if result == "" {
		list = model.ReserveList(model.DB, in)
		marshal, _ := json.Marshal(&list)
		cecha.Rdb.Set(l.ctx, key, marshal, time.Minute*30)

	} else {
		json.Unmarshal([]byte(result), &list)
	}
	return &medirpc.ReserveListResponse{
		List: list,
	}, nil

}
